var MachineInfo = function (machine) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (machine) {
      var fields = ['totalTimeOfDay', 'equipmentImage', 'equipmentNo', 'deptId', 'deptName', 'workShopId', 'workShopName', 'dailyWorkingHours'];
      self.setDataFromObj(machine, fields);

      var machineRecords = machine.equipmentRecordPojos;
      var preMonthCapacityNumber = 0;
      var dateList = _.map(machineRecords, 'createTime');
      var minDate = moment().subtract(29, 'day');
      while (moment().isAfter(minDate, 'day')) {
        var date = minDate.format('YYYY-MM-DD');
        var index = dateList.indexOf(date);
        if (index === -1) {
          machineRecords.push({
            createTime: date,
            machingCount: 0
          });
        } else {
          var machineRecord = machineRecords[index];
          if (machineRecord.machingCount < 0) {
            machineRecord.machingCount = 0;
          }
          if (moment().isSame(date, 'month') && !moment().isSame(date, 'day')) {
            preMonthCapacityNumber += machineRecord.machingCount;
          }
        }
        minDate.add(1, 'day');
      }

      machineRecords = _.orderBy(machineRecords, [function (record) {
        return moment(record.createTime).valueOf();
      }], ['asc']);
      self.machineRecords = machineRecords;

      self.preMonthCapacityNumber = _.round(preMonthCapacityNumber, 0);

      var totalTimeOfMonth = machine.totalTimeOfMonth;
      var totalTimeOfDay = machine.totalTimeOfDay;
      self.beforeTotalTime = totalTimeOfMonth - totalTimeOfDay;

      var dataFields = ['finishDate', 'finishRate', 'totalOfMonth', 'totalOfDay', 'orderNo', 'status', 'need',
        'current', 'timeSingle', 'codeDescribe', 'usedRatioOfMonth', 'usedRatioOfDay'
      ];
      self.setDataFromObj({}, dataFields, '');
    },

    formatData: function () {
      var fields = ['need', 'current', 'totalOfMonth', 'totalOfDay'];
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        if (self[field] < 0) {
          self[field] = 0;
        }
      }
    }
  };

  this.refreshData = function (data) {
    if (data === null) {
      return;
    }
    var fields = ['current', 'need', 'uptime'];
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = data[field];
      if (SysUtil.isEmpty(value)) {
        value = 0;
      } else {
        value = Number(value);
      }
      data[field] = value;
    }

    var totalOfDay = data.count;
    if (SysUtil.isEmpty(totalOfDay)) {
      totalOfDay = 0;
    } else {
      var startProportion = data.startProportion;
      if (SysUtil.isEmpty(startProportion)) {
        startProportion = 1;
      }
      var endProportion = data.endProportion;
      if (SysUtil.isEmpty(endProportion)) {
        endProportion = 1;
      }
      totalOfDay = Math.ceil(_.round(totalOfDay * startProportion / endProportion, 2));
    }
    this.totalOfMonth = _.round(this.preMonthCapacityNumber + totalOfDay, 0);
    this.totalOfDay = totalOfDay;

    var dailyWorkingHours = this.dailyWorkingHours;
    if (!SysUtil.isEmpty(dailyWorkingHours) && dailyWorkingHours > 0) {
      var totalTimeOfDay = data.totalTimeOfDay;
      var totalTimeOfMonth = totalTimeOfDay + this.beforeTotalTime;

      var usedRatioOfMonth = totalTimeOfMonth / (dailyWorkingHours * moment().date()) * 100;
      this.usedRatioOfMonth = _.round(usedRatioOfMonth, 2);
      var usedRatioOfDay = totalTimeOfDay / dailyWorkingHours * 100;
      this.usedRatioOfDay = _.round(usedRatioOfDay, 2);
    } else {
      this.usedRatioOfMonth = '';
      this.usedRatioOfDay = '';
    }

    var totalTimeOfDay = data.totalTimeOfDay;
    var timeSingle = totalTimeOfDay / totalOfDay;
    timeSingle = _.round(timeSingle, 1);
    if (isNaN(timeSingle) || timeSingle < 0) {
      timeSingle = 0;
    }
    this.timeSingle = timeSingle;

    var codeDescribe = data.codeDescribe;
    if (SysUtil.isEmpty(codeDescribe)) {
      codeDescribe = '';
    }
    this.codeDescribe = codeDescribe;

    var need = data.need;
    var current = data.current;
    if (need < current) {
      this.finishDate = '';
    } else {
      var finishDate = moment().add((need - current) * timeSingle, 'second').valueOf();
      this.finishDate = finishDate;
    }

    var finishRate = current / need * 100;
    finishRate = _.round(finishRate, 2);
    if (isNaN(finishRate) || finishRate < 0) {
      finishRate = 0;
    }
    this.finishRate = finishRate;

    this.orderNo = data.order;
    this.setDataFromObj(data, ['status', 'need', 'current', 'factoryType']);

    var status = this.status;
    if (status === '运行') {
      var nowTime = data.nowTime;
      var workTime = data.processMaxWorkingTime;
      if (!SysUtil.isEmpty(nowTime) && !SysUtil.isEmpty(workTime) && workTime > 0) {
        nowTime = moment(nowTime).startOf('second').valueOf();
        var rangeTime = moment().diff(nowTime, 'second');
        if (rangeTime > workTime) {
          status = '待机';
          this.status = status;
        }
      }
    }

    var statusObj = {
      "1": "运行",
      "2": "待机",
      "3": "警报",
      "4": "离线",
      "5": "暂停"
    };
    var statusCode = '';
    for (var code in statusObj) {
      var value = statusObj[code];
      if (status === value) {
        statusCode = code;
        break;
      }
    }
    this.statusCode = statusCode;
    fn.formatData();
  };

  fn.initData(machine);
};
